package com.swordxu.yim.server.tool.mananger;

import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.swordxu.yim.server.dbproxy.model.Session;
import com.swordxu.yim.server.dbproxy.model.User;
import com.swordxu.yim.server.memcache.SessionCache;
import com.swordxu.yim.server.packet.LoginPacket;
import com.swordxu.yim.server.packet.MsgServerPacket;
import com.swordxu.yim.server.session.YimSession;
import com.swordxu.yim.server.utils.Constans;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import org.apache.log4j.Logger;

import java.util.UUID;

/**
 * 登录业务处理类
 * Created by 徐晓明 on 2015/1/7.
 */
public class LoginMananger {
    private Logger logger = Logger.getLogger(LoginMananger.class);
    private static LoginMananger loginMananger;
    private static ChannelHandlerContext channel;

    public static LoginMananger instance(ChannelHandlerContext channel) {
        synchronized (LoginMananger.class) {
            if (loginMananger == null) {
                loginMananger = new LoginMananger();
                loginMananger.channel = channel;
            }
            return loginMananger;
        }
    }

    /**
     * 处理客户端登录请求
     * @param buffer
     */
    public void login(String buffer) {
        logger.debug("有用户发送登录请求!");
        LoginPacket loginPacket = new LoginPacket();
        System.out.println("处理客户端登录请求:" + buffer);
        loginPacket.decode(buffer);
        LoginPacket.PacketRequest request = (LoginPacket.PacketRequest) loginPacket.getmRequest();
        String name = request.entity.name;
        String pwd = request.entity.pwd;
        System.out.println("请求登录用户名:" + name + "----------------密码:" + pwd);

        User user = User.dao.findFirst("select * from t_user where name = '" + name + "' and pwd = '" + pwd + "'");

        LoginPacket.PacketResponse.Entity entity = new LoginPacket.PacketResponse.Entity();

        //如果user=null,用户登录请求失败
        if (user == null) {
            entity.result = Constans.RESULT_LOGIN_FAILURE;  //登录请求失败
        } else {
            entity.result = Constans.RESULT_LOGIN_SUCCESS;  //登录请求成功

            if (SessionCache.read(name) == null) {
//                SessionCache.insert(name, new YimSession(channel.channel()));
            }
        }
        LoginPacket.PacketResponse response = new LoginPacket.PacketResponse(entity);
        response.setUser(user);
        loginPacket.setmResponse(response);
        channel.writeAndFlush(loginPacket.encode());
    }

    /**
     * 获取消息服务器ip
     * @param buffer
     */
    public void reqMsgServer(String buffer) {
        logger.debug("有用户发送请求消息服务器地址!");
        MsgServerPacket packet = new MsgServerPacket();
        packet.decode(buffer);
        MsgServerPacket.PacketResponse.Entity entity = new MsgServerPacket.PacketResponse.Entity();
        entity.result = 0;
        entity.ip1 = "192.168.1.63";
        entity.port = 8992;
        entity.ip2 = "192.168.1.63";
        MsgServerPacket.PacketResponse res = new MsgServerPacket.PacketResponse(entity);
        packet.setmResponse(res);
        channel.writeAndFlush(packet.encode());
        System.out.println("返回消息服务器地址:" + packet.encode());
    }
}
